package com.taotao.tools.enums.jackson;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.BeanProperty;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.ContextualSerializer;
import com.taotao.tools.enums.BaseEnum;
import com.taotao.tools.enums.constant.EnumConstant;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;


/**
 * 枚举集合属性的处理。
 * 集合属性转json,前端不好处理。多加两个字段方面前端处理
 *
 *
 *
 * <pre>
 *     SimpleModule simpleModule = new SimpleModule();
 *  simpleModule.addSerializer(Object[].class,new EnumArraySerializer());
 *  objectMapper.registerModule(simpleModule);
 * </pre>
 */
@Slf4j
@AllArgsConstructor
@NoArgsConstructor
public class EnumArraySerializer extends JsonSerializer<Object[]> implements ContextualSerializer {
    private String fieldName;

    @Override
    public void serialize(Object[] value, JsonGenerator jgen, SerializerProvider provider)
            throws IOException {

        if (null == value || value.length == 0) {
            return;
        }
        // 使用默认的序列化行为
        writeDefaultEnumArr(jgen, value);

        for (Object v : value) {
            if (!(v instanceof BaseEnum)) {
                return;
            }
        }


        writeLabel(jgen, fieldName, value);
        writeCode(jgen, fieldName, value);

    }

    private void writeDefaultEnumArr(JsonGenerator jgen, Object[] value) throws IOException {
        jgen.writeStartArray();
        for (Object v : value) {
            if (v instanceof BaseEnum) {
                BaseEnum e = (BaseEnum) v;
                jgen.writeObject(e);
            } else {
                jgen.writeObject(v);
            }
        }
        jgen.writeEndArray();

    }


    private void writeCode(JsonGenerator jgen, String fieldName, Object[] value) throws IOException {
        jgen.writeFieldName(fieldName + EnumConstant.CODE_FIELD_SUFFIX);
        jgen.writeStartArray();
        for (Object v : value) {
            if (v instanceof BaseEnum) {
                BaseEnum e = (BaseEnum) v;
                jgen.writeObject(e.getCode());
            } else {
                jgen.writeObject(v);
            }
        }
        jgen.writeEndArray();
    }

    private void writeLabel(JsonGenerator jgen, String fieldName, Object[] value) throws IOException {
        jgen.writeFieldName(fieldName + EnumConstant.LABEL_FIELD_SUFFIX);
        jgen.writeStartArray();
        for (Object v : value) {
            if (v instanceof BaseEnum) {
                BaseEnum e = (BaseEnum) v;
                jgen.writeObject(e.getText());
            } else {
                jgen.writeObject(v);
            }
        }
        jgen.writeEndArray();


    }


    @Override
    public JsonSerializer<?> createContextual(SerializerProvider serializerProvider, BeanProperty beanProperty) throws JsonMappingException {
        if (beanProperty == null) {
            //为空直接跳过
            return serializerProvider.findNullValueSerializer(beanProperty);
        }

        return new EnumArraySerializer(beanProperty.getName());
    }
}